//
//  FunctionVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class FunctionVC: BaseVC {
  
  fileprivate var tableView: UITableView!
  fileprivate var titles: [String] = []
  fileprivate var imageNames: [String] = []
  fileprivate var backgroundImageV: UIImageView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = GlobalConfig.BGWhiteColor
    blueNavigationBackgroud()
    titles = ["工单中心", "流程审批", "移动巡检",
              "移动抄表"]
    // MARK: - 01
    imageNames = ["工单中心", "流程审批", "移动巡检",
                  "移动抄表"]
    
    if User.permission_customer_view || User.systemAdmin {//判断权限
        titles.append("客户信息查询")
        imageNames.append("customerInfo")
    }
    
    NotificationCenter.default.addObserver(self, selector: #selector(changePermission), name: NSNotification.Name.changeCompany.changeCompanyUpdateData, object: nil)
    
    create()
  }
  
    @objc func changePermission() {
        
        if User.permission_customer_view || User.systemAdmin {//判断权限
            if !titles.contains("客户信息查询") {
                titles.append("客户信息查询")
            }
            if !imageNames.contains("customerInfo") {
                imageNames.append("customerInfo")
            }
          
        }else{
            
            if titles.contains("客户信息查询") {
                titles.removeObject("客户信息查询")
            }
            if imageNames.contains("customerInfo") {
                imageNames.removeObject("customerInfo")
            }
            
           
        }
        
        tableView.reloadData()
        
    }
    
    
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    GlobalConfig.baiduStatisticsPageViewStart("功能")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    GlobalConfig.baiduStatisticsPageViewEnd("功能")
  }
  
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

}

// MARK: - fileprivate

extension FunctionVC {
  fileprivate func create() {
    
    backgroundImageV = UIImageView()
    view.addSubview(backgroundImageV)
    backgroundImageV.backgroundColor = UIColor.clear
    backgroundImageV.image = UIImage(named: "bg-00")
    backgroundImageV.snp.makeConstraints { (make) in
      make.left.equalTo(40.adaptor)
      make.right.equalTo(-40.adaptor)
      make.bottom.equalTo(-50)
      make.height.equalTo(163.adaptor)
    }
    
    tableView = UITableView()
    tableView.backgroundColor = UIColor.clear
    tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noBarHeight)
    tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.rowHeight = FunctionCell.height
    tableView.register(FunctionCell.self, forCellReuseIdentifier: FunctionCell.identifier)
    tableView.tableFooterView = UIView(frame: CGRectMake(0, 0, UIScreen.width, 50))
    view.addSubview(tableView)

  }
}

// MARK: - UITableViewDataSource

extension FunctionVC : UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return titles.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: FunctionCell.identifier) as! FunctionCell
    cell.functionImage = UIImage(named: imageNames[indexPath.row])
    cell.title = titles[indexPath.row]
    return cell
  }
}

// MARK: - UITableViewDelegate

extension FunctionVC : UITableViewDelegate {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var vc: UIViewController
    
    switch indexPath.row {
    case 0:
      vc = OrderCenterVC()
    case 1:
      vc = FlowCenterVC()
    case 2:
      vc = PatrolInspectionVC()
    case 3:
        vc = MovementReadingMeterVC()
    case 4:
        vc = CustomerSearchVC()
    default:
      return
    }
    
    navigationController?.pushViewController(vc, animated: true)
  }
}
